package org.lib.kafkahello.try1;

import java.util.Date;
import java.util.stream.IntStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * 启动时发送消息
 * @author ben
 * @date 2021-08-24 16:32:15 CST
 */
//@Component
@Slf4j
public class Try1Runner implements CommandLineRunner {

	@Autowired
	private KafkaTemplate<String, Object> kafkaTemplate;
	
	@Override
	public void run(String... args) throws Exception {
		IntStream.range(0, 10).forEach(i->{
			log.info("send msg-{}", i);
			String msg = String.format("Try1Runner send msg-%d @%s", i, new Date());
			// 无key
			kafkaTemplate.send(Try1Config.TOPIC_01, msg);
			// 有key
			kafkaTemplate.send(Try1Config.TOPIC_01, Try1Config.TOPIC_01_KEY, "key-" + msg);
		});
	}

}
